package learn_mybatis.basesqlsession;

import entity.User;
import org.junit.Test;

import java.util.List;
import java.util.Map;

/**
 * 基于 sqlSession，更适合用于学习 mybatis 底层逻辑
 */
public class QuickStartBasedSqlSession extends BaseSqlSessionTest {
    // 测试添加，调用sqlSession的insert方法
    @Test
    public void testInsert() {
        User user1 = new User();
        user1.setId(1);
        user1.setName("tianshouzhi");
        user1.setAge(27);
        User user2 = new User();
        user2.setId(2);
        user2.setName("wangxiaoxiao");
        user2.setAge(26);
        String statement = namespace + ".insert";
        sqlSession.insert(statement, user1);
    }

    // 测试查询单条记录，调用sqlSession的selectOne方法
    @Test
    public void testSelectOne() {
        User user = sqlSession.selectOne(namespace + ".selectById", 1);
        assert user != null;
        System.out.println("id:" + user.getId() + ",name:" + user.getName() + ",age:" + user.getAge());
    }

    // 测试查询多条记录，并将结果封装到一个List中，调用sqlSession的selectList方法
    @Test
    public void testSelectList() {
        List<User> userlist = sqlSession.selectList(namespace + ".selectAll");
        System.out.println(userlist);
    }

    // 测试查询多条记录，并将结果封装到一个Map中，调用sqlSession的selectMap方法
    @Test
    public void testSelectMap() {
        Map<Integer, User> userMap = sqlSession.selectMap(namespace + ".selectAll", "id");
        System.out.println(userMap);
    }

    // 测试更新，调用sqlSession的update方法
    @Test
    public void testUpdate() {
        User user = sqlSession.selectOne(namespace + ".selectById", 2);
        assert user != null;
        user.setName("huhuamin");
        user.setAge(26);
        int updateCount = sqlSession.update(namespace + ".updateById", user);
        assert updateCount == 1;
    }

    // 测试删除，调用sqlSession的delete方法
    @Test
    public void testDelete() {
        int deleteCount = sqlSession.delete(namespace + ".deleteById", 1);
        assert deleteCount == 1;
    }

    // 测试删除，调用sqlSession的delete方法
    @Test
    public void testDeleteAll() {
        int deleteCount = sqlSession.delete(namespace + ".deleteAll");
        //assert deleteCount == 1;
    }

}